#include "Koopa.h"
//#include "Global.h"
#include "Resource.h"

//Koopa::Koopa(float x, float y)
//{
//	check_turn = false;
// 
//	_x = x;
//	_y = y;
//	_vx = (float)KOOPA_VX;
//	_vy = 0;
//}

Koopa::Koopa(LPDIRECT3DDEVICE9 d3ddv,  LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Object_State, int Direction): Object(x, y)//, ID, Object_State)
{
	this->setIDObjType(ID);
	check_turn = false;

	pos_x = x;
	pos_y = y;

	veloc_x = (float)KOOPA_VX;
	veloc_y = 0;
	accel_y = 0;

	_xOld = x;
	_yOld = y;

	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\turtle.png"), 125, 0);

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	timeStop = 0;

	isGravity = true;

	collisionObjectList = new list<Object*>;

	//object_State = ES_ACTIVING;
	obj_state = Object_State;
	direction = Direction;
	if (obj_state == ES_ACTIVING)
	{
		if (direction == 1)			//sang trai
		{
			veloc_x = -(float)KOOPA_VX;
		} 
		else if (direction == 2)	//sang phai
		{
			veloc_x = (float)KOOPA_VX;
		}
	}
	else if (obj_state == ES_STOP)
	{
		timeStop = 0;
		veloc_x = 0;
		p_sprite->setFrame(6,7);
		
	}
	else if (obj_state == ES_MOVE_SHELL_LEFT)
	{
		p_sprite->setFrame(6,8);

		veloc_x = -KOOPA_VX * 5;
		accel_y = GRAVITY;

		 
	}
	else if (obj_state == ES_MOVE_SHELL_RIGHT)
	{
		p_sprite->setFrame(6,8);

		veloc_x = KOOPA_VX * 5;
		accel_y = GRAVITY;


	}
}

Koopa::~Koopa()
{
	/*if (_sprite != NULL)
	delete _sprite;*/
	if (collisionObjectList){
		delete collisionObjectList;
		//collisionObjectList = NULL;
	}
}




void Koopa::render(int cameraX, int cameraY)
{

	if (obj_state != ES_REMOVE)
	{

		//if (object_State == ES_STOP || object_State == ES_MOVE_SHELL_LEFT || object_State == ES_MOVE_SHELL_RIGHT)
		//{
		//	//_spriteShell->Render((int)_x,(int)_y);
		//}
		//else
		//{
		//	
		//}
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}

}

OBJECT_TYPE Koopa::getObjectType()
{
	return OBJECT_ENEMY;
}

/**--------------------------------------------------------
*Description : Turn left Turtle
*Method name : TurnLeft
*Parameters :   
*Return value : void
--------------------------------------------------------**/
void Koopa :: TurnLeft()
{
	check_turn = true;
	 

	if (obj_state == ES_MOVE_SHELL_RIGHT || obj_state == ES_MOVE_SHELL_LEFT)
	{
		 
		p_sprite->setFrame(6, 9);
	}
	else	//state active
	{
		p_sprite->setFrame(0, 2);
		 
	}
}


/**--------------------------------------------------------
*Description : Turn right Turtle
*Method name : TurnRight
*Parameters :   
*Return value : void
--------------------------------------------------------**/
void Koopa :: TurnRight()
{
	check_turn = false;
	 

	if (obj_state == ES_MOVE_SHELL_RIGHT || obj_state == ES_MOVE_SHELL_LEFT)
	{
		 
		p_sprite->setFrame(6, 9);
	}
	else	//state active
	{
		p_sprite->setFrame(2, 4);
		 
	}
}

void Koopa::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	list<Object*>::iterator i;
	Object* obj;

	switch (obj_state)
	{
	case ES_ACTIVING:
		move(obj_list, gameTime);

		if (pos_y <= 0)	//roi xuong ho, height = 0
		{
			changeState(ES_REMOVE);
		}
		p_sprite->update(gameTime);
		break;

	case ES_MOVE_SHELL_LEFT:
	case ES_MOVE_SHELL_RIGHT:
		move(obj_list, gameTime);
		for (i = obj_list->begin(); i != obj_list->end(); i++)
		{
			obj = *i;
			if (obj->getIDObjType() == 11 || (obj->getIDObjType() == 12 && obj != this))
			{
				Box box(this->_xOld,
					this->_yOld - this->height, 
					width, 
					height					
					);
								
				Box block(obj->getPosX(),
					obj->getPosY() - obj->getHeight(),
					obj->getWidth(),
					obj->getHeight()
					);
				if (Collision::AABBCheck(box, block) && (obj->getObjState() == ES_ACTIVING || obj->getObjState() == ES_STOP))
				{
					obj->changeState(ES_FALL);
				}
			}
		}
		if (pos_y <= 0)	//roi xuong ho, height = 0
		{
			changeState(ES_REMOVE);
		}
		p_sprite->update(gameTime);
		//_spriteShell->Update(gameTime);
		//ResetRect();
		break;
	case ES_STOP:
		timeStop += gameTime;
		if (timeStop > 2900)
		{
			timeStop -= 2900;
			changeState(ES_ACTIVING);
		}
		//_spriteShell->Update(gameTime);
		p_sprite->update(gameTime);
		//ResetRect();
		break;
	case ES_FALL:
		pos_x += veloc_x * gameTime;
		veloc_y -= accel_y * gameTime;
		pos_y += veloc_y * gameTime + 1/2 * accel_y * gameTime *gameTime;
		
		if (pos_y <= 0)	//roi xuong ho, height = 0
		{
			changeState(ES_REMOVE);
		}
		p_sprite->update(gameTime);
		break;
	case ES_FALLBULLET:
		pos_x += veloc_x * gameTime;
		veloc_y -= accel_y * gameTime;
		pos_y += veloc_y * gameTime + 1/2 * accel_y * gameTime *gameTime;

		if (pos_y <= 0)	//roi xuong ho, height = 0
		{
			changeState(ES_REMOVE);
		}
		p_sprite->update(gameTime);

		break;
	}


}

void Koopa::move(list<Object*>* obj_list, float TPF)
{
	list<Object*>::iterator i;
	Object* obj;

	_xOld = pos_x;
	_yOld = pos_y;

	/*collisionObjectList->clear();
	for (i = staObjs->begin(); i != staObjs->end(); i++)
	{
		obj = *i;
		if (obj->GetTypeObject() == OBJECT_LAND)
		{
			collisionObjectList->push_back(obj);
		}
	}*/

	//Xet va cham theo truc X
	pos_x += veloc_x * TPF;
	//ResetRect();

	//for (i = staObjs->begin(); i != staObjs->end(); i++)
	//{
	//	obj = *i;
	//	if (Collision::RectVsRect(this->GetBond(), obj->GetBond()))
	//	{
	//		//xet va cham gach, da, ong,...
	//		//if (obj->GetID() >= 21 && obj->GetID() <= 24)
	//		if (obj->GetTypeObject() == OBJECT_LAND)
	//		{
	//			_vx = -_vx;
	//			_x = _xOld;
	//		}
	//	}
	//	ResetRect();
	//}
	 

	//Xet va cham theo truc Y
	if (isGravity == false)
	{
		accel_y = 0;
	}
	else
	{
		accel_y = GRAVITY;
	}	
	veloc_y -= accel_y * TPF;
	pos_y += veloc_y * TPF + 1/2.0 * accel_y * TPF * TPF;

	//rectOld = _rectBond;
	//ResetRect();


	//kiem tra va cham nam tren gach, da
	//if(collisionObjectList->size() > 0)
	//{
	//	Object* obj;
	//	list<Object*>::iterator i;
	//	for(i = collisionObjectList->begin(); i != collisionObjectList->end(); i++)
	//	{
	//		obj = *i;
	//		//kiem tra va cham nam tren gach, da,... khong cho roi xuong
	//		if(this->GetBond().bottom <= obj->GetBond().bottom && this->GetBond().left < obj->GetBond().right && this->GetBond().right > obj->GetBond().left)
	//		//if(rectOld.bottom<obj->GetBond().bottom)
	//			if(obj->GetY() - this->_sprite->texture->_Height < this->_y)
	//			{
	//				_vy = 0;
	//				_y = obj->GetY() - this->GetSprite()->texture->_Height;
	//			}

	//	}
	//	ResetRect();
	//}
	for (i = obj_list->begin(); i != obj_list->end(); i++)
	{
		obj = *i;
		if (obj->getObjectType() == OBJECT_LAND)
		{
			//Box box = this->getBox(time);
			Box box(this->_xOld,
				this->_yOld - this->height, 
				width, 
				height, 
				veloc_x * TPF, 
				veloc_y * TPF + 1.0f/2.0f * accel_y * TPF * TPF);

			//Box block = obj->getBox(time);
			Box block(obj->getPosX(),
				obj->getPosY() - obj->getHeight(),// - 0.11,
				obj->getWidth(),
				obj->getHeight()
				);

			Box boxphase = Collision::GetSweptBroadphaseBox(box);
			if (Collision::AABBCheck(boxphase, block))
			{
				float normalx = 0;
				float normaly = 0;
				float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
				if (0 < collision_time && collision_time < 1)
				{
					isGravity = false;
					if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
					{
						//pos_x = _xOld + veloc_x * collision_time;// + 1.0f/2.0f * accel_x * collision_time *collision_time;
						//accel_x = 0;
						//veloc_x = -veloc_x;

						if (this->_yOld - this->height < obj->getPosY())
						{
							pos_x = _xOld + veloc_x * collision_time;
							veloc_x = -veloc_x;						
						}
					} 
					else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
					{
						//pos_x = _xOld + veloc_x * collision_time;// + 1.0f/2.0f * accel_x * collision_time *collision_time;
						//accel_x = 0;
						//veloc_x = -veloc_x;

						if (this->_yOld - this->height < obj->getPosY())
						{
							pos_x = _xOld + veloc_x * collision_time;
							veloc_x = -veloc_x;						
						}
					}
					else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
					{
						/*if (obj->getIDObjType() == 22)
						{
							isGravity = false;
						}*/
					}
					else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
					{
						

						pos_y = _yOld + veloc_y * collision_time + 1.0f/2.0f * accel_y * collision_time *collision_time;// + 0.5;
						accel_y = 0;
						veloc_y = 0;

						/*if (obj->getIDObjType() == 22)
						{
							isGravity = false;
						}
						else
						{
							isGravity = true;
						}*/
					}

					/*if (this->_yOld - this->height <= obj->getPosY() + 1.5)
					{
						isGravity = false;
					}
					else
					{
						isGravity = true;
					}*/
					 
				}
				else
				{
					isGravity = true;
				}
			}
			else
			{
				isGravity = true;				
			}
						 
		}
		else
		{
			isGravity = true;
		}
	}

	if(veloc_x > 0)
	{
		TurnRight();
	}
	else
	{
		TurnLeft();
	}
	_xOld = pos_x;
	_yOld = pos_y;

	if (veloc_x >= 0)	//sang phai
	{
		direction = 2;
	} 
	else
	{
		direction = 1;
	}
}

void Koopa::changeState(int state)
{
	obj_state = state;
	switch (obj_state)
	{
	case ES_ACTIVING:
		if (check_turn == false)	 //turn right
		{
			veloc_x = KOOPA_VX;
		} 
		else						//turn left
		{
			veloc_x = -KOOPA_VX;
		}
		 
		accel_y = GRAVITY;
		break;

	case ES_FALL:
		if (check_turn == false)	 //turn right
		{
			p_sprite->setFrame(5,6);
		} 
		else						//turn left
		{
			p_sprite->setFrame(4,5);
		}
		veloc_y = 1.5;
		accel_y = GRAVITY;
		 
		break;

	case ES_FALLBULLET:
		if (check_turn == false)	 //turn right
		{
			p_sprite->setFrame(10,10);
		} 
		else						//turn left
		{
			p_sprite->setFrame(9,9);
		}
		//_sprite->SetFrame(9,9);
		veloc_y = 1.5;
		accel_y = GRAVITY;

		break;

	case ES_MOVE_SHELL_LEFT:
		p_sprite->setFrame(6,8);
		pos_x -= 1;
		veloc_x = -KOOPA_VX * 5;
		accel_y = GRAVITY;
		break;
	case ES_MOVE_SHELL_RIGHT:
		p_sprite->setFrame(6,8);
		pos_x += 1;
		veloc_x = KOOPA_VX * 5;
		accel_y = GRAVITY;
		break;

	case ES_STOP:
		timeStop = 0;
		veloc_x = 0;
		 
		p_sprite->setFrame(6,7);
		break;
	}
}

//void Koopa::ResetRect()
//{
//	//dang them vao
//	_rectBond.left = _x;
//	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
//	_rectBond.top = _y;
//	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
//
//}
//
//void Koopa::ResetRectNULL()
//{
//	//dang them vao
//	_rectBond.left = 0;
//	_rectBond.right = _rectBond.left + 0;
//	_rectBond.top = 0;
//	_rectBond.bottom = _rectBond.top + 0;
//
//}